package ooday03;
/** 多态的演示 */
public class PoilmorphicDemo {
    public static void main(String[] args) {
        //演示强转成功的条件
        /*
        Animal o = new Dog("小黑",5,"黑");
        Dog g = (Dog)o;//引用O所指向的对象，就是Dog类型
        Swim s = (Swim)o;//引用O所指向的对象，实现了swim接口
        //Fish f = (Fish)o; //运行时发成ClassCastException
        System.out.println(o instanceof Dog);//true
        System.out.println(o instanceof Swim);//true
        System.out.println(o instanceof Fish );//false
        if(o instanceof Fish){
        }

         */

        //演示向上造型的第2个应用
        Animal[] animal = new Animal[5];
        animal[0] = new Dog("小红",8,"红");
        animal[1] = new Dog("小黄",7,"黄");
        animal[2] = new Fish("小金",1,"金");
        animal[3] = new Fish("大金",5,"金");
        animal[4] = new Chick("小灰",6,"灰");
        for (int i = 0; i < animal.length ; i++) {
            System.out.println(animal[i].age);
            animal[i].eat();
            animal[i].drink();
            //狗看家/狗游泳/鸡下蛋/鱼游泳
            if(animal[i] instanceof Dog ){
                Dog dog = (Dog)animal[i];
                dog.lookHome();
            }
            if (animal[i] instanceof Fish){
                Fish fish = (Fish)animal[i];
            }
            if (animal[i] instanceof Chick ){
                Chick chick = (Chick)animal[i];
                chick.layegg();
            }
            if (animal[i] instanceof Swim){//适用于所有实现Swim接口的(会游泳的)
                Swim s =(Swim) animal[i];
                s.swim();
            }



        }

        //向上造型的语法
        /*

        Dog  o1 = new Dog("小黑",5,"黑");
       //o1.name,age,color,eat(),drink(),swim(),lookHome()

        Animal o2 = new Dog("小黑",5,"黑");//向上造型
        //o2.name,age,color,eat(),drink()

        Swim o3 = new Dog("小黑",5,"黑");
        //o3.swim,

         */
        //演示向上造型第1个应用
        /*
        Master master = new Master();
        Animal dog = new Dog("小黑",5,"黑");
        Animal chick = new Chick("小小白",4,"白");
        Animal fish = new Fish("小金",5,"金");
        master.feed(dog);
        master.feed(chick);
        master.feed(fish);


        Dog dog = new Dog("小黑",5,"黑");
        Chick chick = new Chick("小小白",4,"白");
        Fish fish = new Fish("小金",5,"金");
        master.feed(dog);
        master.feed(chick);
        master.feed(fish);   正常new也可以 我们再Master里面已经造型了 传参的时候，系统自己做了向上造型

 */
    }
}
